<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 具体页面结构 -->
        组织架构
        <el-divider />
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 30px;width: 90%;margin-left:100px;font-size:14px">
          <el-col :span="20">
            <svg-icon icon-class="gongsi1" /> <span style="font-weight:700">江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree
          :data="list"
          :default-expand-all="true"
          style="width: 90%;margin-left:100px;font-size:14px"
        >
          <template v-slot="scope">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="20">
                <svg-icon v-if="scope.data.children.length !== 0" :icon-class="scope.node.expanded ? 'jian' : 'jia'" />
                <svg-icon icon-class="chicken" />
                <span>{{ scope.data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ scope.data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(scope.data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(scope.data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hDel(scope.data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <el-dialog
        :title=" isEdit ? '编辑' : '添加'"
        :visible.sync="showDialog"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
      >
        <deptDialog
          v-if="showDialog"
          :id="curId"
          :is-edit="isEdit"
          :origin-list="originList"
          @close="hClose"
          @success="hSuccess"
        />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getDepartments, delDepartment } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import deptDialog from './deptDialog.vue'
export default {
  components: { deptDialog },
  data() {
    return {
      // 依赖一份树形数据
      list: [],
      showDialog: false,
      curId: '',
      isEdit: false,
      originList: []
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    async loadDepartments() {
      try {
        const res = await getDepartments()
        // 保存数据
        // shift会取出数组第一个元素，同时会改变源数组
        const company = res.data.depts.shift()
        console.log('部门', company)
        // 组装一份简单的列表数据,给子组件用来做表单校验
        this.originList = res.data.depts.map(item => {
          return {
            id: item.id,
            pid: item.pid,
            name: item.name,
            code: item.code
          }
        })
        this.list = tranListToTreeData(res.data.depts)
        // 后端返回的数据中 第一条是公司信息，它应该直接就是tree的根节点
        // 为了后面计算方便，把它直接取出来
      } catch (err) {
        console.log(err)
      }
    },
    async doDel(id) {
      try {
        const res = await delDepartment(id)
        console.log(res)
        this.$message.success('删除成功')
        this.loadDepartments()
      } catch (err) {
        console.log(err)
        this.$message.error('删除失败')
      }
    },
    hDel(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.doDel(id)
        this.loadDepartments()
      }).catch(() => {

      })
    },
    hEdit(id) {
      // 显示对话框
      this.showDialog = true
      // 更新当前操作的id
      this.curId = id
      // 3. 传递一个标识给子组件，告诉它我们现在是编辑状态
      this.isEdit = true
      // 获取子组件的引用
      // this.$nextTick(() => {
      //   console.log('获取子组件的引用', this.$refs.deptDialog)
      //   console.log('当前的curId', id)
      //   console.log('获取到子组件中的id--从父传入的', this.$refs.deptDialog.id)
      //   // 调用子组件的方法
      //   this.$refs.deptDialog.loadDepartmentDetailById()
      // })
    },
    hAdd(id) {
      // 显示对话框
      this.showDialog = true
      // 更新当前操作的id
      this.curId = id
      // 3. 传递一个标识给子组件，告诉它我们现在不是编辑状态
      this.isEdit = false
    },
    hSuccess() {
      // 关闭弹层，再次更新数据
      this.showDialog = false
      this.loadDepartments()
    },
    hClose() {
      this.showDialog = false
    }
  }
}
</script>
